package com.hanyxp.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 字符集过滤
 * @author hanyxp
 *
 */
public class CharEncodeFilter implements Filter {

	private String encode = "utf-8";
	
	public void destroy() {
		this.encode = null;
	}

	public void doFilter(ServletRequest servletReq, ServletResponse servletResp,
			FilterChain filterChain) throws IOException, ServletException {
		servletReq.setCharacterEncoding(this.encode);
		filterChain.doFilter(servletReq, servletResp);
	}

	public void init(FilterConfig cfg) throws ServletException {
		String initParam = cfg.getInitParameter("encode");
		if(initParam!=null)
			this.encode = initParam.toLowerCase(); 
	}

}
